/******************************************************************************
 * Copyright (c) 2008, Intalio Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Intalio Inc. - initial API and implementation
 *******************************************************************************
 * Date         Author             Changes
 * Apr 25, 2008      Antoine Toulme     Created
 */
package ms.unicorn.diagram.borders;

import ms.unicorn.diagram.figures.SliableOvalAnchorEx;
import ms.unicorn.dsl.ITheme;

import org.eclipse.draw2d.ConnectionAnchor;
import org.eclipse.draw2d.EllipseAnchor;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Insets;
import org.eclipse.draw2d.geometry.Rectangle;

/**
 * This scheme border shows a border under the oval shape.
 *
 *
 * @author <a href="http://www.intalio.com">Intalio Inc.</a>
 * @author <a href="mailto:atoulme@intalio.com">Antoine Toulme</a>
 */
public class OvalSchemeBorder extends RoundedSchemeBorder {

	public static final OvalSchemeBorder INSTANCE = new OvalSchemeBorder();
	
    @SuppressWarnings("deprecation")
	@Override
    protected void fillShadow(IFigure fig, Graphics graphics, Insets insets, ITheme theme) {
        Rectangle rect = fig.getBounds().getCopy().translate(2, 2)
                    .resize(-4, -4);//getPaintRectangle(fig, insets);

        graphics.fillOval(rect.getCopy().crop(insets));
    }
    
    @Override
    public void paint(IFigure fig, Graphics graphics, Insets insets, ITheme theme) {
    	graphics.pushState();
    	try {
	         if (theme.isShadow()) {
	        	 graphics.setAlpha(theme.getShadowAlpha());
	             graphics.setBackgroundColor(theme.getShadowColor());
	             fillShadow(fig, graphics, insets, theme);
	         }         
	         graphics.setAlpha(theme.getBackgroundAlpha());
	         graphics.setBackgroundColor(theme.getBackgroundColor());
	         graphics.setForegroundColor(theme.getForeColor());
	         
	         Rectangle rt = getAdjustedBound(fig, theme);
	         if (theme.isShadow()) 
	        	 rt.resize(-4, -4);
	         graphics.fillOval(rt);
	         graphics.drawOval(rt);
    	}
    	finally {
    		graphics.popState();
    	}
         return;
    }
    
    @Override
    public Class<? extends ConnectionAnchor> getConnectionAnchorClass() {
    	return SliableOvalAnchorEx.class;
    }
    
    @Override
    public ConnectionAnchor createAnchor(IFigure figure) {
    	return new EllipseAnchor(figure);
    }
}
